package com.cneport.transfer.router.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cneport.transfer.router.model.User;
import com.cneport.transfer.router.support.service.UserService;
import com.cneport.transfer.util.IdGenerate;

/**
 * 用户管理
 * @author wudi 2012-7-18
 */
@Controller
@RequestMapping("/clientManager")
public class ClientController {

    /**
     * 用户信息service
     */
    @Autowired
    private UserService userService;

    /** LOG 日志操作类 */
    private static final Log LOG = LogFactory.getLog(ClientController.class);

    /**
     * 查询用户信息列表
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/queryClientInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryClientInfo(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap();
        User user = new User();

        try {
            String nodeId = request.getParameter("nodeId");
            String clientName = request.getParameter("clientName");
            String status = request.getParameter("status");
            String start = request.getParameter("start");
            String limit = request.getParameter("limit");

            user.setNodeId(nodeId);
            user.setClientName(clientName);
            user.setStatus(status);
            user.setStart(Integer.parseInt(start));
            user
                    .setLimit(Integer.parseInt(start) + Integer.parseInt(limit)
                            + 1);
            List<User> userList = userService.getUserListService(user);
            responseMap.put("success", true);
            responseMap.put("root", userList);
            responseMap.put("totalProperty", userService
                    .countUserListService(user));
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }
        return responseMap;
    }

    /**
     * 查询用户信息
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/getClientInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getClientInfo(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap();
        try {
            String clientId = request.getParameter("clientId");
            User user = userService.getUserByClientId(clientId);
            responseMap.put("data", user);
            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }
        return responseMap;
    }

    /**
     * 根据flag更新或者保存用户信息
     * @param request 请求
     * @param response 应答
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/saveOrUpdateUser", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveOrUpdateUser(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap();
        User user = new User();
        try {
            String clientId = request.getParameter("clientId");
            String clientName = request.getParameter("clientName");
            String orgId = request.getParameter("orgId");
            String nodeId = request.getParameter("nodeId");
            String transType = request.getParameter("transType");
            String transAddr = request.getParameter("transAddr");
            String linkMan = request.getParameter("linkMan");
            String linkTel = request.getParameter("linkTel");
            String linkAddr = request.getParameter("linkAddr");
            String status = request.getParameter("status");
            String flag = request.getParameter("flag");

            user.setClientId(clientId);
            user.setClientName(clientName);
            user.setIndbTime(new Date());
            user.setLinkAddr(linkAddr);
            user.setLinkMan(linkMan);
            user.setLinkTel(linkTel);
            user.setNodeId(nodeId);
            user.setOrgId(orgId);
            user.setStatus(status);
            user.setTransAddr(transAddr);
            user.setTransType(transType);

            if ("add".equals(flag)) {
                user.setId(IdGenerate.getUUIDString());
                userService.insertUserService(user);
            } else if ("edit".equals(flag)) {
                userService.updateUserService(user);
            }
            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }
        return responseMap;
    }

    /**
     * 删除用户
     * @param request 请求
     * @param response 响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteUser(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap();
        String clientId = request.getParameter("clientId");
        try {
            userService.deleteUserService(clientId);
            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }
        return responseMap;
    }
}
